package jjn.carl.linkedlist;

import commons.LinkedListHelper;
import commons.ListNode;

import java.util.List;

/**
 * @author Jiang Jining
 * @since 2023-06-30 21:51
 */
public class LeetCode203_WithoutDummy {
    public ListNode removeElements(ListNode head, int val) {
        if (head == null) {
            return null;
        }
        while (head != null) {
            if (head.val == val) {
                head = head.next;
            } else {
                break;
            }
        }
        ListNode start = head;
        
        while (start != null && start.next != null) {
            if (start.next.val == val) {
                start.next = start.next.next;
            } else {
                start = start.next;
            }
        }
        return head;
    }
    
    public static void main(String[] args) {
        ListNode listNode = LinkedListHelper.buildLinkedList(List.of(6, 6, 6, 6, 6));
        ListNode node = new LeetCode203_WithoutDummy().removeElements(listNode, 6);
        while (node != null) {
            System.out.println("node.val = " + node.val);
            node = node.next;
        }
    }
}
